package com.smalluz.ad.modules.oe.api.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.smalluz.ad.modules.common.config.MediaConfigStorage;
import com.smalluz.ad.modules.oe.api.OeOpenService;
import com.smalluz.ad.modules.oe.api.OeService;
import com.smalluz.ad.modules.oe.api.OpenComponentService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class OpenComponentServiceImpl implements OpenComponentService {

    private static final Map<String, OeService> OE_SERVICE_MAP = new ConcurrentHashMap<>();

    private final OeOpenService oeOpenService;

    public OeOpenService getOpenService() {
        return oeOpenService;
    }

    @Override
    public OeService getOeServiceByAdvertiseId(String advertiseId) {
        log.info("~~~~获取advertiseId Service ： {}",advertiseId);
        OeService oeService = OE_SERVICE_MAP.get(advertiseId);
        if (oeService == null) {
            synchronized (OE_SERVICE_MAP) {
                oeService = OE_SERVICE_MAP.get(advertiseId);
                if (oeService == null) {
                    MediaConfigStorage configProvider = this.getOpenMediaConfigStorage();
                    oeService = new OeServiceOkHttpImpl();
                    oeService.setConfigStorage(configProvider);
                    OE_SERVICE_MAP.put(advertiseId, oeService);
                }
            }
        }
        return oeService;
    }

    @Override
    public MediaConfigStorage getOpenMediaConfigStorage() {
        return this.getOpenService().getMediaConfigStorage();
    }

}
